package com.dartfar.base.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.dartfar.base.dao.DaoUtil;
import com.dartfar.base.model.Authority;
import com.dartfar.base.model.Function;
import com.dartfar.base.model.Role;
import com.dartfar.menu.model.Menu;

@Component("authorityService")
public class AuthorityServiceImpl implements AuthorityService {

	@Resource
	private DaoUtil<Authority> authorityDaoUtil;
	@Resource
	private DaoUtil<Function> functionDaoUtil;

	/**
	 * 保存权限功能
	 * @param roleId 角色ID
	 * @param subMenuId 子菜单ID
	 * @param functionIds 功能字符串
	 * @return 返回添加功能的ID
	 */

	@Override
	public void add(Role role, Menu menu, String functionIds) {
		Authority authority = null;
		if(functionIds == null){
			authority = new Authority();
			authority.setRole(role);
			authority.setMenu(menu);
			authority.setFunction(null);
			authorityDaoUtil.save(authority);
		}else{
			//根据功能ID获取相应 权限列表
			System.out.println("from Function f where f.id in ("+ functionIds +")");
			List<Function> functions = functionDaoUtil.getList("from Function f where f.id in ("+ functionIds +")");
			for(Iterator<Function> i = functions.iterator();i.hasNext();){
				Function funcation = i.next();
				authority = new Authority();
				authority.setRole(role);
				authority.setMenu(menu);
				authority.setFunction(funcation);
				authorityDaoUtil.save(authority);
			}
		}

	}

	public boolean deleteByRoleId(int roleId){
		Boolean flag = true;
		try{
			authorityDaoUtil.execute("delete Authority a where a.role.roleId = " + roleId);
		}catch (Exception e) {
			// TODO: handle exception
			flag = false;
		}
		return flag;
	}
	
	
	/**
	 * 获取所有权限列表
	 * @return 所有权限的列表
	 */
	@Override
	public List<Authority> getList() {
		// TODO Auto-generated method stub
		return authorityDaoUtil.getList("from Authority order by id asc");
	}

	
	/**
	 * 根据条件获取权限列表
	 * @param roleId 角色ID
	 * @param menuId 菜单ID
	 * @return 当前条件下的功能列表
	 */
	@Override
	public List<Authority> getListByRoleMenuId(String roleIds, int roleId, int menuId) {
		String hql = "from Authority a where 1 = 1";
		if(roleId != 0){
			hql = hql + " and a.role.roleId = " + roleId;
		}
		if(menuId != 0){
			hql = hql + " and a.menu.id = " + menuId;
		}
		if(roleIds != null){
			hql = hql + " and a.role.roleId in (" + roleIds + ")";
		}
		
		hql = hql + " order by id asc";
		return authorityDaoUtil.getList(hql);
	}
	
	/**
	 * 根据权限列表获取主菜单方法
	 * @param role 角色ID
	 */
	@Override
	public List<Menu> getMenuByAuthoritys(List<Authority> authoritys){
		//List<Authority> autList = getListByRoleMenuId(0, 0, roleIds);
		List<Menu> menus = new ArrayList<Menu>();
		for(Iterator<Authority> a = authoritys.iterator();a.hasNext();){
				Authority aut = a.next();
				menus.add(aut.getMenu().getMainMenu());
		}
		return menus;
	}
	
	/**
	 * 根据权限列表获取子菜单方法
	 * @param autList 权限列表
	 */
	@Override
	public List<Menu> getSubMenuByAuthorityList(List<Authority> authoritys) {
		List<Menu> menus = new ArrayList<Menu>();
		for(Iterator<Authority> a = authoritys.iterator();a.hasNext();){
				Authority aut = a.next();
				menus.add(aut.getMenu());
		}
		return menus;
	}
	
}
